package com.piece.blog.service;

import com.piece.blog.model.BlogQuestion;
import com.piece.blog.repository.BlogQuestionRepository;
import com.piece.core.framework.enums.StatusType;
import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.util.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@Service(value = "blogQuestionService")
public class BlogQuestionService {

    @Autowired
    private BlogQuestionRepository blogQuestionRepository;

    public BlogQuestion insert(BlogQuestion question) {
        return blogQuestionRepository.save(question);
    }

    public BlogQuestion update(BlogQuestion question) {
        return blogQuestionRepository.saveOrUpdate(question.getQuestionId(), question);
    }

    public void delete(String questionId) {
        BlogQuestion question = new BlogQuestion();
        question.setQuestionId(questionId);
        blogQuestionRepository.delete(question);
    }

    public BlogQuestion get(String id) {
        return blogQuestionRepository.get(id);
    }

    public List<BlogQuestion> findList(QueryTable queryTable) {
        DataTable dataTable = findPage(queryTable);
        return dataTable.getData();
    }

    public List<BlogQuestion> findByGroup(String group) {
        QueryTable queryTable = new QueryTable();
        queryTable.setStart(0);
        queryTable.setLength(0);
        Map<String, Object> param = new HashMap<>();
        param.put("Q_group_S_EQ", group);
        param.put("Q_status_S_EQ", StatusType.NORMAL.getCode());
        queryTable.setParams(param);
        List<Order> orders = new ArrayList<>();
        orders.add(new Order("position", SqlKey.ASC));
        queryTable.setOrder(orders);
        return findList(queryTable);
    }

    public DataTable findPage(QueryTable queryTable) {
        return blogQuestionRepository.findPage(queryTable);
    }

    /**
     * 查询所有问题所属
     */
    public List<String> findAllGroup() {
        QueryTable queryTable = new QueryTable();
        queryTable.setGroup("group");
        List<String> result = new ArrayList<>();
        List<Map> list = blogQuestionRepository.findByGroup(queryTable, Map.class);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(map -> {
                result.add(map.get("group").toString());
            });
        }
        return result;
    }

    /**
     * 获取下一个问题序号
     */
    public int getNextPosition(String group) {
        QueryTable queryTable = new QueryTable();
        queryTable.setStart(0);
        queryTable.setLength(1);
        Map<String, Object> param = new HashMap<>();
        param.put("Q_group_S_EQ", group);
        queryTable.setParams(param);
        List<Order> orders = new ArrayList<>();
        orders.add(new Order("position", SqlKey.DESC));
        queryTable.setOrder(orders);
        List<BlogQuestion> questions = findList(queryTable);
        if (CollectionUtil.isNotEmpty(questions)) {
            return questions.get(0).getPosition() + 1;
        } else {
            return 1;
        }
    }
}
